//商品编辑展示
function showEdit(wxUserId) {
    $.ajax({
        url : "user/query",
        async : true,
        type : "GET",
        data : {
            "wxUserId" : wxUserId
        },
        // 成功后开启模态框
        success : queryEdit,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function queryEdit(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#e_wxUserId").val(model.wxUserId);
    $("#e_userId").text(model.wxUserId);
    $("#e_nickname").text(model.nickname);
    $("#e_account").val(model.account);
    $("#e_rewardPoint").val(model.rewardPoint);
    // 显示模态框
    $('#editModal').modal('show');
}

//商品编辑提交
$("#editButton").click(function () {
    var data = new FormData(document.getElementById("editForm"));
    var account = data.get('account');
    var accountReg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(!accountReg.test(account)){
        alert('请输入正确的金额！');
        return;
    }
    $.ajax({
        type: 'post',
        url: 'user/modify',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("修改用户账户信息成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});

//商品删除
function showDelete(wxUserId) {
    if (confirm("是否确认删除该用户？")) {
        $.ajax({
            type: 'get',
            url: 'user/delete',
            dataType: 'json',
            data: {
                "wxUserId": wxUserId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("删除用户成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


/**
 * 评论展示
 * @param wxUserId
 * @param pageIndex
 * @param num
 * @param totalPages
 */
function showCoupon(wxUserId) {
    $.ajax({
        url : "user/coupon",
        async : true,
        type : "GET",
        data : {
            "wxUserId": wxUserId
        },
        // 成功后显示表格数据
        success : function(res) {
            if (res.code == 0) {
                var list = res.data;
                if(list.length==0){
                    alert("该用户暂无优惠券");
                    return;
                }

                //填充表格数据
                var tableHtml = "";
                for(var i = 0; i<list.length; i++){
                    var model = list[i];
                    var range;
                    switch (model.temRange) {
                        case 0: range="不限"; break;
                        case 1: range="食品/保健类"; break;
                        case 2: range="服饰类"; break;
                        case 3: range="皮具箱包类"; break;
                        case 4: range="珠宝饰品类"; break;
                        case 5: range="母婴/儿童类"; break;
                        case 6: range="家电类"; break;
                        case 7: range="数码类"; break;
                        case 8: range="汽车配件类"; break;
                        case 9: range="家具/建材类"; break;
                        case 10: range="图书影音类"; break;
                    }
                    tableHtml += '<tr>' +
                        '<td>' + model.temName + '</th>' +
                        '<td>' + model.temNumber + ' 元</td>' +
                        '<td>' + model.temBottomPrice + ' 元</td>' +
                        '<td>' + range + '</td>' +
                        '<td>' + model.temTimespan + ' 天</td>' +
                        '<td>' + model.couponTime + '</td>' +
                        '<td>' + model.endTime + '</td>' +
                        '</tr>';
                }
                $("#comment-table").html(tableHtml);

                // 显示模态框
                $('#couponModal').modal('show');

            } else {
                alert(res.msg);
            }
        },
        error : function(res) {
            alert("优惠券获取失败");
        },
        dataType : "json"
    });
}

